package main

import (
	"fmt"
	"math/rand"
	"sort"
	"time"
)

type Student struct {
	Name  string
	Age   int
	Score float64
}
type students []Student

// 对学生成绩按照升序排列
// func Sort(data Interface)
func (stu students) Len() int {
	return len(stu)
}

// // Less方法报告索引i的元素是否比索引j的元素小
func (stu students) Less(i, j int) bool {
	if stu[i].Score == stu[j].Score {
		return stu[i].Name < stu[j].Name
	} else {
		return stu[i].Score < stu[j].Score
	}
}

func (stu students) Swap(i, j int) {
	stu[i], stu[j] = stu[j], stu[i]
}

func main() {
	var stus students
	rand.Seed(time.Now().Unix())
	for i := 0; i < 20; i++ {
		n := rand.Intn(200) + 1
		stu := Student{
			Name:  fmt.Sprintf("%d-%v", i, n),
			Age:   n,
			Score: float64(n) / 20.0,
		}
		stus = append(stus, stu)
	}

	for _, val := range stus {
		fmt.Println(val)
	}

	sort.Sort(stus)

	for _, val := range stus {
		fmt.Println(val)
	}
}
